package com.zy.yun.controller;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 王彦委
 * @Description: 测试
 * @Date: 18:16 2019/10/26
 */
@RestController
public class TokenController {

    private static final CloseableHttpClient httpclient = HttpClients.createDefault();

    @RequestMapping("getToken")
    public String getToken(String code) throws IOException {

        List<NameValuePair> formparams = new ArrayList<NameValuePair>();

        formparams.add(new BasicNameValuePair("grant_type", "authorization_code"));
        formparams.add(new BasicNameValuePair("code", code));
        formparams.add(new BasicNameValuePair("redirect_uri", "http://www.baidu.com"));
        formparams.add(new BasicNameValuePair("scope", "all"));
        formparams.add(new BasicNameValuePair("client_id", "client_1"));
        formparams.add(new BasicNameValuePair("client_secret", "123456"));



        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);

        HttpPost httpPost = new HttpPost("http://localhost:8080/oauth/token");
        httpPost.setEntity(entity);
        CloseableHttpResponse response = httpclient.execute(httpPost);
        HttpEntity entity1 = response.getEntity();
        String s = EntityUtils.toString(entity1);
        System.out.println(s);

        return "ok";
    }
}
